#include "common.hpp"

int main()
{
    // 创建共享内存
    int shmid = CreateShm();
    // 挂接当前进程与共享内存
    char *shmaddr = (char *)shmat(shmid, nullptr, 0);

    Init init;
    int fd=open(FIFO_FILE,O_RDONLY);
    if(fd<0)
    {
        log(Fatal,"server error.error string:%s, error code:%d\n",strerror(errno),errno);
        exit(FIFO_OPEN_ERR);
    }
    struct shmid_ds shmds; // 储存共享内存属性的数据结构
    while (1)
    {

        char c;
        size_t s=read(fd,&c,1);
        if(s<=0) break;
        cout << "client say@: ";
        cout << shmaddr << endl;
        sleep(1);
        shmctl(shmid, IPC_STAT, &shmds);
        cout << "shm size:" << shmds.shm_segsz << endl;
        cout << "shm nattch:" << shmds.shm_nattch << endl;
        printf("shm __key:0x%x\n",shmds.shm_perm.__key);
        //cout << "shm __key:" << shmds.shm_perm.__key << endl;
        cout << "shm mode:" << shmds.shm_perm.mode << endl;
    }

    // 分离当前进程与共享内存
    shmdt(shmaddr);
    // 删除共享内存
    shmctl(shmid, IPC_RMID, nullptr);
    
    close(fd);
    return 0;
}